/*
Project: 		The Impact of Short-Term Employment for Low-Income Youth: Experimental 	
				Evidence from the Philippines
Authors: 		Emily A. Beam and Stella Quimbo

***********************************
Path: 			code/02_analysis
File name: 		table_a09.do 
***********************************

Purpose: 		Generates Appendix Table A09 

Inputs: 		spes_data.dta
Outputs: 		table_a09.tex

Log: 			results/03_output/log_table_a09.log		

*/

cap log close 
log using "$output/log_table_a09.log",replace


estimates clear

use "$usedata_analysis/spes_data",clear 
	keep if endline == 1



*** Set up local variables 

local l_bb_female "Female"
local l_NT_edu "College"

local intlist "_bb_female _NT_edu"

local n: word count $cov1
	assert `n' == 18 

** Interaction terms 

foreach interact in  `intlist' {
	
	foreach mes in tr spes_2016{
gen `mes'X`interact' = `mes'*`interact'
label var `mes'X`interact' "SPES X `l`interact''"

	}

assert !missing(`interact')

gen `interact'xtreatment = `interact'*treatment
label var `interact'xtreatment "SPES X `l`interact''"


* Generate inteaction terms for each covariate 

ds $cov1 
foreach cov in `r(varlist)'{ 
gen `interact'X`cov' = `cov'*`interact'
} 

}


loc depvar1 "" 
foreach sub in edu experience fewjobs app con dis{

gen _jsa_`sub' = jobsearch_affect_`sub' == 4 | jobsearch_affect_`sub' == 5
	replace _jsa_`sub' = . if missing(jobsearch_affect_`sub')
	loc depvar1 "`depvar1' _jsa_`sub'"
}

di "`depvar1'"


#delimit cr

* Job search

local labels1 " "Insufficient education"  "Insufficient experience" "Few available jobs" "Difficult application process" "Few contacts" "Discrimination""  



										
loc w = 1				
local name1 "js-affect"
forval j = 1/1{



*foreach mes in tr spes_2016{
local r replace



** Uninteracted version ** 
	foreach var in `depvar`j'' {
	
	
foreach mes in  spes_2016{

		xi:  ivregress 2sls  `var' (`mes' = treatment ) $cov1  i.scel if treatment!=. ,  robust
		estimates store iv`mes'`var'
		summ `var' if treatment==0 
		estadd scalar dmean = `r(mean)'


}
	
	}
	

	foreach var in `depvar`j'' {


*foreach interact in  _bb_female _NT_edu _bb_edulvl{
foreach interact in `intlist'  {

		
foreach mes in  spes_2016{

	
	qui	xi:  ivregress 2sls  `var' (`mes' `mes'X`interact' = treatment `interact'xtreatment) $cov1 `interact'X* i.scel if treatment!=. & _f`interact' == 0,  robust
		
		estimates store iv`mes'`w'`interact'
		di in red "estimates stored as iv`mes'`w'`interact'"
		summ `var' if treatment==0 
		estadd scalar dmean = `r(mean)'
		
		test `mes' + `mes'X`interact' == 0 
		local pval = `r(p)'
		estadd scalar pvalint = `r(p)'
}	// nature of IV loop
		

	
	}		// interaction term
	
	local w = `w' + 1
}		// individual dependent variables within each set
			*/ 
			
			
}		// dependent variable sets (tables)	





********************************************
********************************************
** Nature of job-search  

** Table numbers 


loc tnjsearch = 11 // aspirations


*** Footnote text 


local fn`tnjsearch' = "\multicolumn{7}{p{1.3\textwidth}}{\footnotesize{Notes: All endline respondents included. All specifications include controls listed in Table \ref{tbal} along with stratification-cell fixed effects. Panels B and C add controls multiplied by the binary interaction term along with uninteracted stratification-cell fixed effects. \textit{*** p$<$0.01, ** p$<$0.05, * p$<$0.10} }}\\"


********************************************
********************************************
cd "$tables_analysis"

#delimit ;
include "$dofiles/PanelCombine.do";

********** Appendix Table XX - Job Search   *************;


	
	// Output Tables ;

	

foreach mes in  spes_2016{;
	
	esttab iv`mes'_jsa_edu iv`mes'_jsa_experience iv`mes'_jsa_fewjobs iv`mes'_jsa_app 
			iv`mes'_jsa_con iv`mes'_jsa_dis 
	using table`tnjsearch'_iv_`mes'.tex, replace star(* 0.10 ** 0.05 *** 0.01) 
	cells("b(fmt(3) star)" "se(par([ ]))") 
	stats() noobs
	keep(`mes') label  
	varwidth(16) modelwidth(13) style(tex) 
	title(Impact of SPES on labor-market perceptions and aspirations\label{tasp}) 
	varlabels(_cons Constant) 
mlabels("Insufficient education"  "Insufficient experience" "Few available jobs" "Difficult application process" "Few contacts" "Discrimination", 
				span prefix(\multicolumn{@span}{x{0.13\textwidth}}{) suffix(})) 
	collabels(none)
	mgroups("Reports that  X will affect job search much or very much", pattern(1 0 0 0 0 0 0 0) span prefix(\multicolumn{@span}{c}{) suffix(}) erepeat(\cline{@span}))
	prehead( "\begin{tabular}{lcccccc}" "\toprule") 
	posthead(\hline) 
	prefoot() 
	postfoot("\bottomrule"  );
};





	//* Panel B Female;
	
foreach mes in  spes_2016{;

	esttab iv`mes'1_bb_female 		iv`mes'2_bb_female		iv`mes'3_bb_female		iv`mes'4_bb_female  		  iv`mes'5_bb_female		iv`mes'6_bb_female  			  
	using table`tnjsearch'_iv_`mes'_bb_female.tex, replace star(* 0.10 ** 0.05 *** 0.01) 
	cells("b(fmt(3) star)" "se(par([ ]))") 
	stats( pvalint , fmt(%9.3f  )  
	labels(  "p-value, SPES + SPES X Female" ))  
	keep(`mes' `mes'X_bb_female) label  
	varwidth(16) modelwidth(13) style(tex) 
	title(Impact of SPES on employment\label{temployment}) 
	varlabels(_cons Constant) 
mlabels("Insufficient education"  "Insufficient experience" "Few available jobs" "Difficult application process" "Few contacts" "Discrimination", 
			span prefix(\multicolumn{@span}{x{0.13\textwidth}}{) suffix(})) 
	collabels(none)
	prehead("\begin{table}\caption{@title}" "\begin{center}"  "\begin{tabular}{lcccccc}" "\toprule") 
	posthead(\hline) 
	prefoot() 
	postfoot("\bottomrule"  );
};
	//* Panel C Education ;
	
foreach mes in  spes_2016{;

	esttab iv`mes'1_NT_edu		iv`mes'2_NT_edu 			iv`mes'3_NT_edu		iv`mes'4_NT_edu  	iv`mes'5_NT_edu		iv`mes'6_NT_edu  			  
	using table`tnjsearch'_iv_`mes'_NT_edu.tex, replace star(* 0.10 ** 0.05 *** 0.01) 
	cells("b(fmt(3) star)" "se(par([ ]))") 
	stats(pvalint N   dmean, fmt(%9.3f %9.0f %9.3f )  
	labels("p-value, SPES + SPES X College" "Observations"   "Mean, control group")) 
	keep(`mes' `mes'X_NT_edu) label  varwidth(16) modelwidth(13) style(tex) 
	title(Impact of SPES on employment\label{temployment}) 
	varlabels(_cons Constant) 
	mlabels("Insufficient education"  "Insufficient experience" "Few available jobs" "Difficult application process" "Few contacts" "Discrimination", 
			span prefix(\multicolumn{@span}{x{0.13\textwidth}}{) suffix(})) 
	collabels(none)
	prehead("\begin{table}\caption{@title}" "\begin{center}"  "\begin{tabular}{lcccccc}" "\toprule") 
	posthead(\hline) prefoot() postfoot("\bottomrule" "`fn`tnjsearch''" "\end{tabular}" );
};



#delimit ;



// Table 5  - IV - self report ;
panelcombine, use(table`tnjsearch'_iv_spes_2016.tex table`tnjsearch'_iv_spes_2016_bb_female.tex table`tnjsearch'_iv_spes_2016_NT_edu.tex)  
columncount(5)  paneltitles("Aggregate treatment effects" "Interacted by gender" "Interacted by education level") 
save(table_a09.tex) cleanup;


log close; 

exit;


